package com.kim.user.web.controller;

import com.kim.common.resp.CodeMsg;
import com.kim.common.resp.Result;
import com.kim.user.common.entity.SysUser;
import com.kim.user.web.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Mr.Yangxiufeng
 * Date: 2018-06-13
 * Time: 10:27
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findByUsername")
//    @HystrixCommand(fallbackMethod = "findByUsernameHandler")
    public Result findByUsername(@RequestParam String username) {
        log.info(" into --> findByUsername");
//        int i = 1 / 0 ;
        SysUser user = userService.findByUsername(username);
        return user == null ? Result.error(CodeMsg.USER_NOT_EXIST) : Result.success(user);
    }


    @GetMapping("/insert")
    public Result insertUser(@RequestParam String userName) {
//        log.info("find username : " + userName);
        SysUser byUsername = userService.findByUsername(userName);
        if (byUsername != null) {
            log.info("user 校验不通过");
            return Result.error(CodeMsg.USER_NAME_EXIST);
        }
        log.info("insert username : " + userName);
        SysUser sysUser = new SysUser();
        sysUser.setUsername(userName);
        int i = userService.insertUser(sysUser);
        if (i != 1) {
            return Result.error(CodeMsg.SAVE_FAIL);
        } else {
            return Result.success("保存成功");
        }
    }
}
